package com.zcl.app.adapter;

import java.util.List;

import com.zcl.funnygame.R;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageView.ScaleType;

public class AdvAdapter extends BaseAdapter {

	private List<Integer> apps;
	private Context ctx;
	
	public AdvAdapter(List<Integer> apps,Context ctx)
	{
		this.apps = apps;
		this.ctx = ctx;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return Integer.MAX_VALUE;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return apps.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@SuppressWarnings("deprecation")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ImageView rImage = new ImageView(ctx);
		if(apps.size()>0){
			int id = position % apps.size();
			Integer ab = apps.get(id);
			try {
				Drawable icon = ctx.getResources().getDrawable(ab);
				if(icon != null){
					rImage.setBackgroundDrawable(icon);
				}else{
					rImage.setImageResource(R.drawable.adv_default);
				}
			} catch (Exception e) {
				rImage.setBackgroundResource(R.drawable.adv_default);
			}	
		}else{
			rImage.setBackgroundResource(R.drawable.adv_default);
		}
		rImage.setAdjustViewBounds(true);
		rImage.setLayoutParams(new Gallery.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		rImage.setScaleType(ScaleType.FIT_XY);
		return rImage;	
		
	}

}
